﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using EventPlanner.Models;

namespace EventPlanner.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EventosAJAX
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [WebGet]
        [OperationContract]
        public List<EventoJson> Listado(int cant, int comienzo)
        {
            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;
            EventoJson.Server = uri.Host+":"+uri.Port;
            EventPlannerContext context = new EventPlannerContext();
            return EventoJson.EventoList2Json(
                (from e in context.Evento orderby e.Fecha select e).Skip(comienzo).Take(cant).ToList()
            ) ;
        }

        [WebGet]
        [OperationContract]
        public List<EventoJson> ProximosEventos(int cant)
        {
            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;
            EventoJson.Server = uri.Host + ":" + uri.Port;
            EventPlannerContext context = new EventPlannerContext();
            return EventoJson.EventoList2Json(
                (from e in context.Evento where e.Fecha >= DateTime.Today orderby e.Fecha select e).Take(cant).ToList()
            );
        }
    }
}
